const {
    addProducts,
    getList
} = require('../service/products.service')
const {error} = require("../constant/err.type");
const {success, text} = require("../constant");
class ProductsController {
    async add (ctx, next) {
        try {
            const res = await addProducts ({...ctx.request.body, userId: ctx.state.user.id}, 'create')
            ctx.body = {
                code: success,
                message: text.success,
                result: {
                    id: res.id,
                    userName: res.userName
                }
            }
        } catch (err) {
            ctx.app.emit('error', error, ctx)
        }
    }
    async getProductsList (ctx) {
        try {
            const {pageNum = 1, pageSize = 10} = ctx.request.body
            const res = await getList(pageNum, pageSize)
            if (res) ctx.body = {
                code: success,
                message: text.success,
                result: res
            }
        } catch (err) {

        }
    }
    async upData (ctx) {
        try {
            const res = await addProducts ({...ctx.request.body, userId: ctx.state.user.id}, 'upData', ctx)
            if (res) {
                ctx.body = {
                    code: success,
                    message: text.success
                }
            }
        } catch (err) {
            ctx.app.emit('error', error, ctx)
        }
    }
}
module.exports = new ProductsController()
